<?php


namespace App\Http\Controllers\Documents;

use App\Http\Controllers\common\LMReturn;
use App\Models\TbDocumentsClassify;
use App\Models\Worker;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class utils
{
    // 判断是否有文章管理权限
    public static function if_power($worker_id) {
        $worker = Worker::where('id', $worker_id)->with('roles.powers')->first();
        if (isset($worker['roles'])) {
            foreach ($worker['roles'] as $role) {
                if (isset($role['powers'])) {
                    foreach ($role['powers'] as $power) {
                        if ($power['id'] == 1) {
                            return 1;
                        }
                    }
                }
            }
        }
        return 0;
    }

    // 文章类型添加
    public static function add_classify($new_classify, $p_id, $h_id) {
        if ($p_id == null && $h_id == null) {
            $where[] = ['p_id', null];
            $where[] = ['h_id', null];
            $where[] = ['tb_documents_classify.id', '!=', $new_classify->id];
            $classify = TbDocumentsClassify::where($where)->first();
            $classify->h_id = $new_classify->id;
            $classify->save();
        } elseif ($p_id != null && $h_id == null) {
            $classify = TbDocumentsClassify::where('p_id', $p_id)->where('h_id', null)->first();
            if ($classify) {
                $classify->h_id = $new_classify->id;
                $classify->save();
            }
            $new_classify->p_id = $p_id;
            $new_classify->save();
        } elseif ($h_id != null) {
            $classify = TbDocumentsClassify::where('h_id', $h_id)->first();
            $new_classify->p_id = $classify->p_id;
            $new_classify->h_id = $classify->h_id;
            $new_classify->save();
            $classify->h_id = $new_classify->id;
            $classify->save();
        }
    }

    // 文章类型删除
    public static function del_classify($classify_id) {
        DB::beginTransaction();
        try {
            $classify = TbDocumentsClassify::where('id', $classify_id)->with('tb_documents_classifies')->with('tb_documents')->first();
            if (count($classify['tb_documents']) > 0) {
                return LMReturn::Error('无法删除，存在该类型的文章');
            }
            if (count($classify['tb_documents_classifies']) > 0) {
                return LMReturn::Error('无法删除，存在类型存在子类型');
            }
            $h_classify = TbDocumentsClassify::where('h_id', $classify_id)->first();
            if ($h_classify) {
                $h_classify->h_id = $classify->h_id;
                $h_classify->save();
            }
            $classify->delete();
            DB::commit();
            return LMReturn::Success();
        } catch (\Throwable $th) {
            DB::rollBack();
            return LMReturn::Error('请检查参数');
        }
    }

    // 文章类型修改
    public static function update_classify($up_id, $refer_id, $up_type) {
        DB::beginTransaction();
        try {
            $up_classify = TbDocumentsClassify::find($up_id);
            $h_classify = TbDocumentsClassify::where('h_id', $up_id)->first();
            if ($h_classify) {
                $h_classify->h_id = $up_classify->h_id;
                $h_classify->save();
            }
            if ($up_type == 1) {
                $refer_classify = TbDocumentsClassify::find($refer_id);
                $up_classify->p_id = $refer_classify->p_id;
                $up_classify->h_id = $refer_classify->h_id;
                $refer_classify->h_id = $up_id;
                $refer_classify->save();
            } elseif ($up_type == 2) {
                $refer_classify = TbDocumentsClassify::where('h_id', $refer_id)->first();
                if ($refer_classify) {
                    $up_classify->p_id = $refer_classify->p_id;
                    $up_classify->h_id = $refer_classify->h_id;
                    $refer_classify->h_id = $up_id;
                    $refer_classify->save();
                } else {
                    $refer_up_classify = TbDocumentsClassify::find($refer_id);
                    $up_classify->p_id = $refer_up_classify->p_id;
                    $up_classify->h_id = $refer_id;
                }
            } elseif ($up_type == 3) {
                $refer_classify = TbDocumentsClassify::where('p_id', $refer_id)->where('h_id', null)->first();
                $up_classify->h_id = null;
                $up_classify->p_id = $refer_id;
                if ($refer_classify) {
                    $refer_classify->h_id = $up_id;
                    $refer_classify->save();
                }
            } else {
                return LMReturn::Error('操作方式传入不对');
            }
            $up_classify->save();
            DB::commit();
            return LMReturn::Success();
        } catch (\Throwable $th) {
            DB::rollBack();
            return LMReturn::Error('请检查参数是否正确');
        }
    }

    // 文章类型树状图查询
    public static function sele_classify($classify_id=null) {
        try {
            $all_classifies = TbDocumentsClassify::orderBy('tb_documents_classify.p_id', 'asc')->get();
            $p_classify_dic = self::rank_pid($all_classifies);
            $tree_classify = [];
            if ($classify_id) {
                if (isset($p_classify_dic[$classify_id])) {
                    $top_classify = $p_classify_dic[$classify_id];
                } else {
                    $top_classify = [];
                }
            } else {
                $top_classify = $p_classify_dic[''];
            }
            if ($top_classify) {
                foreach ($top_classify as $classify) {
                    $tree_list = self::get_tree_classify($p_classify_dic, $classify['id']);
                    if ($tree_list) {
                        $classify['tree_list'] = $tree_list;
                    }
                    $tree_classify[] = $classify;
                }
            }
            if ($classify_id) {
                foreach ($p_classify_dic as $key => $values) {
                    foreach ($values as $value) {
                        if ($value['id'] == $classify_id) {
                            $this_classify = $value;
                            if ($tree_classify) {
                                $this_classify['tree_list'] = $tree_classify;
                            }
                            return $this_classify;
                        }
                    }
                }
            }
            return $tree_classify;
        } catch (\Throwable $th) {
            return [];
        }
    }

    // 按pid分类并且按hid排序
    public static function rank_pid($classifys) {
        $classify_list = [];
        $classify_dic_list = [];
        foreach ($classifys as $classify) {
            $classify_dic['id'] = $classify->id;
            $classify_dic['name'] = $classify->name;
            $classify_dic['weight'] = $classify->weight;
            $classify_dic['p_id'] = $classify->p_id;
            $classify_dic['h_id'] = $classify->h_id;
            if (empty($classify_dic_list)) {
                $classify_dic_list[] = $classify_dic;
            } else {
                if ($classify_dic_list[0]['p_id'] == $classify->p_id) {
                    $classify_dic_list[] = $classify_dic;
                } else {
                    $classify_list[$classify_dic_list[0]['p_id']] = self::rank_hid($classify_dic_list, null, []);
                    $classify_dic_list = [];
                    $classify_dic_list[] = $classify_dic;
                }
            }
        }
        $classify_list[$classify_dic_list[0]['p_id']] = self::rank_hid($classify_dic_list, null, []);
        return $classify_list;
    }

    // 按hid排序
    public static function rank_hid($pid_dic_list, $h_id, $new_dic) {
        foreach ($pid_dic_list as $pid_dic) {
            if ($pid_dic['h_id'] == $h_id) {
                $new_dic[] = $pid_dic;
                if (count($new_dic) == count($pid_dic_list)) {
                    return $new_dic;
                }
                return self::rank_hid($pid_dic_list, $pid_dic['id'], $new_dic);
            }
        }
        return $new_dic;
    }

    // 得到文章分类的树
    public static function get_tree_classify($p_classify_dic, $top_classify_id) {
        if (isset($p_classify_dic[$top_classify_id])) {
            $p_classify_list = $p_classify_dic[$top_classify_id];
            $new_p_list = [];
            foreach ($p_classify_list as $p_classify) {
                $tree_list = self::get_tree_classify($p_classify_dic, $p_classify['id']);
                if ($tree_list) {
                    $p_classify['tree_list'] = $tree_list;
                }
                $new_p_list[] = $p_classify;
            }
            return $new_p_list;
        } else {
            return [];
        }
    }

    // 得到文章分类所有子类
    public static function get_child_classify($classify_id) {
        $child_classify = self::sele_classify();
        return self::get_child($child_classify, $classify_id);
    }

    // 递归查找文章分类子类
    public static function get_child($child_list, $classify_id) {
        foreach ($child_list as $child) {
            if ($child['id'] == $classify_id) {
                return $child;
            } else {
                if (isset($child['tree_list'])) {
                    $child_lists = self::get_child($child['tree_list'], $classify_id);
                    if ($child_lists) {
                        return $child_lists;
                    }
                }
            }
        }
        return [];
    }

    // 仅返回文章子类的所有id列表(一维数组形式)
    public static function get_child_list($classify_id) {
        $child_classify_dic = self::get_child_classify($classify_id);
        if (isset($child_classify_dic['tree_list'])) {
            return self::get_child_id($child_classify_dic['tree_list'], []);
        } else {
            return [];
        }
    }

    // 递归遍历查找子类id
    public static function get_child_id($child_list, $lists) {
        foreach ($child_list as $child) {
            $lists[] = $child['id'];
            if (isset($child['tree_list'])) {
                $lists = self::get_child_id($child['tree_list'], $lists);
            }
        }
        return $lists;
    }

    // 删除图片方法
    public static function del_img($img_path) {
        $num = 0;
        while ($num < 3) {
            // 1 删除 文件图片
            $exists = Storage::disk('new_ftp')->delete('htdocs/' . $img_path);
            if ($exists) {
                return 1;
            } else {
                $num += 1;
            }
        }
        return 2;
    }

    // 结果分页
    public static function Paging($document_list, $page, $size) {
        $start = ($page - 1) * $size;
        $article = array_slice($document_list, $start, $size);
        $datas_list = [];
        $count = count($document_list);
        $datas_list['total'] = $count;
        $datas_list['data'] = $article;
        return $datas_list;
    }

}
